package com.android.pdd.baseapp.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Point;
import android.os.Build;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;


public class AlipayDialog extends Dialog implements View.OnClickListener {
    TextView tvDialogTip;
    TextView tvTitle;
    TextView btnDialogCancle;
    TextView btnDialogOk;
    protected WindowManager.LayoutParams params;

    public AlipayDialog(Context context) {
        super(context);
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        LinearLayout linearLayout = new LinearLayout(context);
        linearLayout.setBackgroundColor(0xffffffff);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        WindowManager.LayoutParams params = new WindowManager.LayoutParams();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = WindowManager.LayoutParams.MATCH_PARENT;
        linearLayout.setPadding(dp2px(20), dp2px(20), dp2px(20), dp2px(20));
        linearLayout.setLayoutParams(params);

        tvDialogTip = new TextView(context);
        tvDialogTip.setText("提示");
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        tvDialogTip.setTextColor(0xff000000);
        tvDialogTip.setTextSize(17);
        tvDialogTip.setLayoutParams(layoutParams);
        linearLayout.addView(tvDialogTip);

        tvTitle = new TextView(context);
        tvTitle.setText("提示信息");
        layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        tvTitle.setTextColor(0xff000000);
        tvTitle.setTextSize(16);
        tvTitle.setLayoutParams(layoutParams);
        linearLayout.addView(tvTitle);

        LinearLayout linearLayout1 = new LinearLayout(context);
        linearLayout1.setOrientation(LinearLayout.HORIZONTAL);
        layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.topMargin = dp2px(20);
        layoutParams.gravity = Gravity.RIGHT;
        linearLayout1.setLayoutParams(layoutParams);

        btnDialogCancle = new TextView(context);
        btnDialogCancle.setText("取消");
        layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        btnDialogCancle.setTextSize(16);
        btnDialogCancle.setLayoutParams(layoutParams);
        btnDialogCancle.setVisibility(View.GONE);
        btnDialogCancle.setPadding(dp2px(10), dp2px(7), dp2px(10), dp2px(7));
        linearLayout1.addView(btnDialogCancle);


        btnDialogOk = new TextView(context);
        btnDialogOk.setText("确定");
        layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        btnDialogOk.setTextSize(16);
        btnDialogOk.setTextColor(0xff008B8B);
        btnDialogOk.setLayoutParams(layoutParams);
        btnDialogOk.setVisibility(View.GONE);
        btnDialogOk.setPadding(dp2px(10), dp2px(7), dp2px(10), dp2px(7));
        linearLayout1.addView(btnDialogOk);

        linearLayout.addView(linearLayout1);
        setContentView(linearLayout);

        btnDialogCancle.setOnClickListener(this);
        btnDialogOk.setOnClickListener(this);

        setWidth(ScreenUtils.getScreenWidth(context) * 24 / 30);
        setCancelable(false);
        setCanceledOnTouchOutside(false);
    }

    public int dp2px(final float dpValue) {
        final float scale = Resources.getSystem().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    public static AlipayDialog newInstance(Activity activity) {
        return new AlipayDialog(activity);
    }


    public AlipayDialog tip(CharSequence charSequence) {
        tvDialogTip.setText(charSequence);
        return this;
    }

    public AlipayDialog title(CharSequence charSequence) {
        tvTitle.setText(charSequence);
        return this;
    }

    public AlipayDialog setCancleButton(OnClickListener clickListener) {
        return setCancleButton(null, clickListener);
    }

    public AlipayDialog setCancleButton(CharSequence text) {
        return setCancleButton(text, null);
    }

    public AlipayDialog setCancleButton(CharSequence text, final OnClickListener clickListener) {
        btnDialogCancle.setVisibility(View.VISIBLE);
        if (!TextUtils.isEmpty(text)) {
            btnDialogCancle.setText(text);
        }
        if (clickListener != null) {
            btnDialogCancle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    clickListener.onClick(AlipayDialog.this, 0);
                }
            });
        } else if (TextUtils.isEmpty(text)) {
            btnDialogCancle.setVisibility(View.GONE);
        }
        return this;
    }

    public AlipayDialog setOkButton(OnClickListener clickListener) {
        return setOkButton(null, clickListener);
    }

    public AlipayDialog setOkButton(CharSequence text) {
        return setOkButton(text, null);
    }


    public AlipayDialog setOkButton(CharSequence text, final OnClickListener clickListener) {
        btnDialogOk.setVisibility(View.VISIBLE);
        if (!TextUtils.isEmpty(text)) {
            btnDialogOk.setText(text);
        }
        if (clickListener != null) {
            btnDialogOk.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    clickListener.onClick(AlipayDialog.this, 0);
                }
            });
        }
        return this;
    }

    @Override
    public void onClick(View view) {
        dismiss();
    }

    /**
     * 宽充满屏幕
     *
     * @return
     */
    public AlipayDialog fullWidth() {
        if (params == null) {
            params = getWindow().getAttributes();
        }
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        getWindow().setAttributes(params);
        return this;
    }

    /**
     * 高充满屏幕
     *
     * @return
     */
    public AlipayDialog fullHeight() {
        if (params == null) {
            params = getWindow().getAttributes();
        }
        params.height = WindowManager.LayoutParams.MATCH_PARENT;
        getWindow().setAttributes(params);
        return this;
    }

    /**
     * 高充满屏幕
     *
     * @return
     */
    public AlipayDialog fullScreen() {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        return this;
    }


    /**
     * 位置在屏幕最底部
     *
     * @return
     */
    public AlipayDialog bottom() {
        if (params == null) {
            params = getWindow().getAttributes();
        }
        params.gravity = Gravity.BOTTOM;
        getWindow().setAttributes(params);
        return this;
    }

    /**
     * 位置在屏幕最底部
     *
     * @return
     */
    public AlipayDialog bottom(int marginBottom) {
        if (params == null) {
            params = getWindow().getAttributes();
        }
        params.gravity = Gravity.BOTTOM;
        params.y = marginBottom;
        getWindow().setAttributes(params);
        return this;
    }

    /**
     * 位置在右上角
     *
     * @return
     */
    public AlipayDialog rightTop() {
        if (params == null) {
            params = getWindow().getAttributes();
        }
        params.gravity = Gravity.RIGHT | Gravity.TOP;
        getWindow().setAttributes(params);
        return this;
    }

    /**
     * 位置在右上角
     *
     * @return
     */
    public AlipayDialog leftTop() {
        if (params == null) {
            params = getWindow().getAttributes();
        }
        params.gravity = Gravity.LEFT | Gravity.TOP;
        getWindow().setAttributes(params);
        return this;
    }

    /**
     * 设置宽度
     *
     * @return
     */
    public AlipayDialog setWidth(int width) {
        if (params == null) {
            params = getWindow().getAttributes();
        }
        params.width = width;
        getWindow().setAttributes(params);
        return this;
    }

    /**
     * 设置高度
     *
     * @return
     */
    public AlipayDialog setHeight(int height) {
        if (params == null) {
            params = getWindow().getAttributes();
        }
        params.height = height;
        getWindow().setAttributes(params);
        return this;
    }

    public static class ScreenUtils {
        public static int getScreenWidth(Context context) {
            WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            Point point = new Point();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                //noinspection ConstantConditions
                wm.getDefaultDisplay().getRealSize(point);
            } else {
                //noinspection ConstantConditions
                wm.getDefaultDisplay().getSize(point);
            }
            return point.x;
        }

        /**
         * Return the height of screen, in pixel.
         *
         * @return the height of screen, in pixel
         */
        public static int getScreenHeight(Context context) {
            WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            Point point = new Point();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                //noinspection ConstantConditions
                wm.getDefaultDisplay().getRealSize(point);
            } else {
                //noinspection ConstantConditions
                wm.getDefaultDisplay().getSize(point);
            }
            return point.y;
        }
    }
}
